package com.seawaterbt.ssm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seawaterbt.ssm.entity.*;
import com.seawaterbt.ssm.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ZDatHeatSourceOutlet {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    CzDatHeatSourceOutletDataService datHeatSourceOutletDataService;
    @Autowired
    CzBasHeatsourceExitService basHeatsourceExitService;
    @Autowired ZDatHeatSourceOutletDataService zDatHeatSourceOutletDataService;
    @Autowired CzDatHeatSourceOutletDataHistoryService czDatHeatSourceOutletDataHistoryService;

    public void sendData() {
        QueryWrapper<CzDatHeatSourceOutletData> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("heat_source_outlet_code", 130902008).or().likeRight("heat_source_outlet_code", 130903007);
        List<CzDatHeatSourceOutletData> czData = datHeatSourceOutletDataService.list(queryWrapper);
        Date date = new Date();
        for (CzDatHeatSourceOutletData czdatData : czData) {
            CzBasHeatsourceExit czBasData = basHeatsourceExitService.listOne(czdatData.getHeatSourceOutletCode());
            if (czBasData != null && czBasData.getCodeProvince() != null && !czBasData.getCodeProvince().equals("")) {
                QueryWrapper wrapper = new QueryWrapper();
                wrapper.eq("HeatSourceOutletID", czBasData.getCodeProvince());
                wrapper.eq("GetTime", czdatData.getGetTime());
                ShengDatHeatSourceOutletData byCodeTime = zDatHeatSourceOutletDataService.getByCodeTime(wrapper);
                if (byCodeTime == null) {
                    ShengDatHeatSourceOutletData datData = new ShengDatHeatSourceOutletData();
                    datData.setHeatSourceOutletId(czBasData.getCodeProvince());
                    datData.setAddTime(date);
                    datData.setGetTime(czdatData.getGetTime());
                    datData.setBackPre(Double.valueOf(czdatData.getBackPre()));
                    datData.setBackTemp(Double.valueOf(czdatData.getBackTemp()));
                    datData.setCumulativeHeat(Double.valueOf(czdatData.getCumulativeHeat()));
                    datData.setInstantaneousFlow(Double.valueOf(czdatData.getInstantaneousFlow()));
                    datData.setSupplyPre(Double.valueOf(czdatData.getSupplyPre()));
                    datData.setSupplyTemp(Double.valueOf(czdatData.getSupplyTemp()));
                    zDatHeatSourceOutletDataService.save(datData);
                }
            }
        }

        System.out.println("完成同步热源出口到中间库 " + df.format(new Date()));
    }

    public void getData(){
        QueryWrapper<ShengDatHeatSourceOutletData> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("HeatSourceOutletID", 130900011).or().likeRight("HeatSourceOutletID", 130900013);
        List<ShengDatHeatSourceOutletData> shengDatHeatSourceOutletDataList = zDatHeatSourceOutletDataService.list(queryWrapper);
        for (ShengDatHeatSourceOutletData shengDatHeatSourceOutletData:shengDatHeatSourceOutletDataList){
            CzBasHeatsourceExit one = basHeatsourceExitService.GetOneByProviencecode(shengDatHeatSourceOutletData.getHeatSourceOutletId());
            if (one != null){
                QueryWrapper wrapper = new QueryWrapper();
                wrapper.eq("heat_source_outlet_code", one.getCode());
                wrapper.eq("get_time", shengDatHeatSourceOutletData.getGetTime());
                CzDatHeatSourceOutletData byCodeTime = datHeatSourceOutletDataService.getByCodeTime(wrapper);
                if (byCodeTime == null){
                    CzDatHeatSourceOutletData czDatHeatSourceOutletData = new CzDatHeatSourceOutletData();
                    czDatHeatSourceOutletData.setBackPre(shengDatHeatSourceOutletData.getBackPre()+"");
                    czDatHeatSourceOutletData.setBackTemp(shengDatHeatSourceOutletData.getBackTemp()+"");
                    czDatHeatSourceOutletData.setCumulativeHeat(shengDatHeatSourceOutletData.getCumulativeHeat()+"");
                    czDatHeatSourceOutletData.setGetTime(shengDatHeatSourceOutletData.getGetTime());
                    czDatHeatSourceOutletData.setHeatSourceOutletCode(one.getCode());
                    czDatHeatSourceOutletData.setInstantaneousFlow(shengDatHeatSourceOutletData.getInstantaneousFlow()+"");
                    czDatHeatSourceOutletData.setSupplyPre(shengDatHeatSourceOutletData.getSupplyPre()+"");
                    czDatHeatSourceOutletData.setSupplyTemp(shengDatHeatSourceOutletData.getSupplyTemp()+"");
                    datHeatSourceOutletDataService.saveOrUpdate(czDatHeatSourceOutletData);
                    CzDatHeatSourceOutletDataHistory czDatHeatSourceOutletDataHistory = new CzDatHeatSourceOutletDataHistory();
                    czDatHeatSourceOutletDataHistory.setBackPre(shengDatHeatSourceOutletData.getBackPre()+"");
                    czDatHeatSourceOutletDataHistory.setBackTemp(shengDatHeatSourceOutletData.getBackTemp()+"");
                    czDatHeatSourceOutletDataHistory.setCumulativeHeat(shengDatHeatSourceOutletData.getCumulativeHeat()+"");
                    czDatHeatSourceOutletDataHistory.setGetTime(shengDatHeatSourceOutletData.getGetTime());
                    czDatHeatSourceOutletDataHistory.setHeatSourceOutletCode(one.getCode());
                    czDatHeatSourceOutletDataHistory.setInstantaneousFlow(shengDatHeatSourceOutletData.getInstantaneousFlow()+"");
                    czDatHeatSourceOutletDataHistory.setSupplyPre(shengDatHeatSourceOutletData.getSupplyPre()+"");
                    czDatHeatSourceOutletDataHistory.setSupplyTemp(shengDatHeatSourceOutletData.getSupplyTemp()+"");
                    czDatHeatSourceOutletDataHistoryService.save(czDatHeatSourceOutletDataHistory);
                }
            }
        }
        System.out.println("完成热源出口数据拉取"+new Date());
    }
}
